/**
RoomBot, a IRC bot.
Copyright (C) 2009 Joshua Lückers

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package com.googlecode.roombot.mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import com.googlecode.roombot.util.ConfigHandler;
import java.sql.Statement;
import java.sql.ResultSet;

/**
 * This class deals with MySQL stuff (connecting, queries etc.).
 * @author Joshua Lückers
 * @version 09-11-2009
 */
public class MySQLHandler {
    private static MySQLHandler mysqlHandler;
    private Connection connection;
    private Boolean connected = false;

    static {
        mysqlHandler = new MySQLHandler();
    }

    /**
     * Creates a new MySQLHandler. It also creates the connection to the MySQL
     * server when it is not connected already. When the MySQL driver creates the
     * connection to the MySQL server it will set connected to true.
     */
    public MySQLHandler() {
        if (!connected) {
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                //connect to the mysql server: jdbc:mysql://sever/database,user,passwd
                connection = DriverManager.getConnection("jdbc:mysql://" +
                                                                                ConfigHandler.getInstance().getMysqlServer()+ "/" +
                                                                                ConfigHandler.getInstance().getMysqlDB(),
                                                                                ConfigHandler.getInstance().getMysqlUser(),
                                                                                ConfigHandler.getInstance().getMysqlPasswd());
                if (!connection.isClosed()) {
                    //If the connection is not closed mark it as connected.
                    //This way we dont have to re-initialize the connection all the time.
                    connected = true;
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * Is there a connection with the MySQL server?
     * @return Boolean Whether it is connected.
     */
    public Boolean isConnected() {
        return connected;
    }

    /**
     * Execute a query with a SELECT statement.
     * @param query The select query to execute
     * @return ResultSet The result from the select query
     */
    public ResultSet selectQuery(String query) {
        ResultSet rs = null;
        try {
            Statement stmt = connection.createStatement();
            rs = stmt.executeQuery(query);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return rs;
    }

    /**
     * Get the MySQLHandler instance.
     * @return MySQLHandler The MysqlHandler instance
     */
    public static MySQLHandler getInstance() {
        return mysqlHandler;
    }

}